/**
 * @Author wywinstonwy
 * @Date 2022/1/2 11:36 上午
 * @Description:
 */

import 'package:demo202112/utils/common_appbar.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class MyFormPage extends StatefulWidget {
  const MyFormPage({Key? key}) : super(key: key);

  @override
  _MyFormPageState createState() => _MyFormPageState();
}

class _MyFormPageState extends State<MyFormPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar('Form相关'),
      body: Column(
        children: [
          _buildLogin1(),
        ],
      ),
    );
  }
  _buildFormF(){
       return Form(
         autovalidateMode: AutovalidateMode.always,
         onChanged: () {
           Form.of(primaryFocus!.context!)!.save();
         },
         child: Wrap(
           children: List<Widget>.generate(5, (int index) {
             return Padding(
               padding: const EdgeInsets.all(8.0),
               child: ConstrainedBox(
                 constraints: BoxConstraints.tight(const Size(300, 50)),
                 child: TextFormField(
                   onSaved: (String? value) {
                     print('Value for field $index saved as "$value"');
                   },
                   decoration: InputDecoration(
                     hintText: '电话号码$index',
                   ),
                 ),
               ),
             );
           }),
         ),
       );
  }
  _buildFormField1(){
    return Center(
      child: Shortcuts(
        shortcuts: const <ShortcutActivator, Intent>{
          // Pressing space in the field will now move to the next field.
          SingleActivator(LogicalKeyboardKey.space): NextFocusIntent(),
        },
        child: FocusTraversalGroup(
          child: Form(
            autovalidateMode: AutovalidateMode.always,
            onChanged: () {
              Form.of(primaryFocus!.context!)!.save();
            },
            child: Wrap(
              children: List<Widget>.generate(5, (int index) {
                return Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: ConstrainedBox(
                    constraints: BoxConstraints.tight(const Size(200, 50)),
                    child: TextFormField(
                      onSaved: (String? value) {
                        print('Value for field $index saved as "$value"');
                      },
                    ),
                  ),
                );
              }),
            ),
          ),
        ),
      ),
    );
  }
  _buildTextFormField(){
    return TextFormField(
      decoration: InputDecoration(
        hintText: '电话号码',
      ),
      autovalidateMode: AutovalidateMode.always,
      validator: (value) {
        RegExp reg = new RegExp(r'^\d{11}$');
        if (!reg.hasMatch(value!)) {
          return '请输入11位手机号码';
        }
        return null;
      },
      onSaved: (text){
        print('$text');
      },
    );
  }
  _buildLogin(){
    var _account;
    var _pwd;
    final _formKey = GlobalKey<FormState>();
    return Form(
      key: _formKey,
      child: Column(
        children: [
          TextFormField(
            autovalidateMode: AutovalidateMode.always,
            decoration: InputDecoration(hintText: '请输入账号'),
            onSaved: (value){
              _account = value;
            }, validator: (value) {
            RegExp reg = new RegExp(r'^.{4}$');
            if (!reg.hasMatch(value!)) {
              return '账号最少4个字符';
            }
            return null;
              },
          ),
          //---------------
          TextFormField(
            autovalidateMode: AutovalidateMode.always,
            decoration: InputDecoration(hintText: '输入密码'),
            obscureText: true,
            onSaved: (value) {
              _pwd = value;
            },
            validator: (value) {

              return value!.length >= 6 ? null : '密码最少6个字符';
            },
          ),
          //登录
          ElevatedButton(
              onPressed: (){
            var _state = Form.of(context);
            if(_state!.validate()){
              _state.save();
              _login(_account,_pwd);
            }
          },
              child: Text('登录')
          )

        ],
      ),

    );

  }
  _login(account,pwd){
    print("账号：$account\n密码：$pwd");
  }

  _buildLogin1(){
    var _account;
    var _pwd;
    final _formKey = GlobalKey<FormState>();
    return Form(
      key: _formKey,
      onWillPop: ()async{
        return await showDialog(
            context: context,
            builder: (BuildContext context){
              return AlertDialog(
                title: Text('提示'),
                content: Text('确认退出吗？'),
                actions: [
                  ElevatedButton(onPressed: (){
                    Navigator.of(context).pop(false);
                  }, child: Text('取消')),
                  ElevatedButton(onPressed: (){
                    Navigator.of(context).pop(true);

                  }, child: Text('确定')),
                ],
              );
            });
      },
      child: Column(
        children: [
          TextFormField(
            autovalidateMode: AutovalidateMode.always,
            decoration: InputDecoration(hintText: '请输入账号'),
            onSaved: (value){
              _account = value;
            }, validator: (value) {
            RegExp reg = new RegExp(r'^.{4}$');
            if (!reg.hasMatch(value!)) {
              return '账号最少4个字符';
            }
            return null;
          },
          ),
          //---------------
          TextFormField(
            autovalidateMode: AutovalidateMode.always,
            decoration: InputDecoration(hintText: '输入密码'),
            obscureText: true,
            onSaved: (value) {
              _pwd = value;
            },
            validator: (value) {

              return value!.length >= 6 ? null : '密码最少6个字符';
            },
          ),
          //登录
          ElevatedButton(
              onPressed: (){
                var _state = Form.of(context);
                if(_state!.validate()){
                  _state.save();
                  _login(_account,_pwd);
                }
              },
              child: Text('登录')
          )

        ],
      ),

    );

  }

}


